package com.macro.mall.product.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.api.client.util.Maps;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.product.dto.PmProdSpecDto;
import com.macro.mall.product.dto.PmProductDto;
import com.macro.mall.product.dto.PmProductViewDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;
import java.util.Map;

/**
 * 属性管理Controller
 * Created by macro on 2018/4/26.
 */
@Slf4j
@Controller
@Api(tags = "PmProductController", description = "商品管理")
@RequestMapping("/product")
public class PmProductController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${host.mall.product}")
    private String HOST_MALL_PRODUCT;

    private String SERVICE_NAME="/product";

    @ApiOperation(value = "商品列表--不分页")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmProdSpecDto>> list(@RequestParam(value = "prodSpecName", defaultValue = "") String prodSpecName,
                                                    @RequestParam(value = "serverSpecId", defaultValue = "") Long serverSpecId
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/list";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("prodSpecName",prodSpecName);
        param.put("serverSpecId",serverSpecId);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmProductController.list   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "商品列表--分页")
    @RequestMapping(value = "/listPage", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmProdSpecDto>> listPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                            @RequestParam(value = "prodSpecName", defaultValue = "") String prodSpecName,
                                                            @RequestParam(value = "serverSpecId", defaultValue = "") Long serverSpecId
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/listPage";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("prodSpecName",prodSpecName);
        param.put("serverSpecId",serverSpecId);
        param.put("pageNum",pageNum);
        param.put("pageSize",pageSize);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmProductController.listPage   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);

        return responseEntity.getBody();
    }

    @ApiOperation(value = "商品新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult add(@Validated @RequestBody PmProductDto pmProductDto, BindingResult result) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/add";
        ResponseEntity<CommonResult> responseEntity = restTemplate.postForEntity(url, pmProductDto, CommonResult.class);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "商品修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@Validated @RequestBody PmProductDto pmProductDto, BindingResult result) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/update";
        ResponseEntity<CommonResult> responseEntity = restTemplate.postForEntity(url, pmProductDto, CommonResult.class);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "商品状态修改")
    @RequestMapping(value = "/changeStatus", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult changeStatus(@RequestParam(value = "prodId", defaultValue = "") Long prodId ,
                                     @RequestParam(value = "prodCode", defaultValue = "") String prodCode,
                                     @RequestParam(value = "status", defaultValue = "") Integer status,
                                     @RequestParam(value = "updateDesc", defaultValue = "") String updateDesc ){

        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/changeStatus";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("prodId",prodId);
        param.put("prodCode",prodCode);
        param.put("status",status);
        param.put("updateDesc",updateDesc);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmProductController.changeStatus   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);

        return responseEntity.getBody();
    }
    @ApiOperation(value = "获取单个产品")
    @RequestMapping(value = "/getProductById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmProductDto> getProductById(@RequestParam(value = "prodId", defaultValue = "") Long prodId
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/getProductById";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("prodId",prodId);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmProductController.getProductById   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "商品列表--组合条件分页")
    @RequestMapping(value = "/listPageByMixture", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<Map>> listPageByMixture(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                           @RequestParam(value = "productName", defaultValue = "") String productName,
                                                           @RequestParam(value = "channelId", defaultValue = "") Long channelId,
                                                           @RequestParam(value = "status", defaultValue = "") Integer status
    ){
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/listPageByMixture";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("productName",productName);
        param.put("channelId",channelId);
        param.put("status",status);
        param.put("pageNum",pageNum);
        param.put("pageSize",pageSize);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmProductController.listPageByMixture   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "获取单个产品视图")
    @RequestMapping(value = "/getProdViewById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmProductViewDto> getProdViewById(@RequestParam(value = "prodId", defaultValue = "") Long prodId
    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/getProdViewById";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("prodId",prodId);

        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmProductController.getProdViewById   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }
    @ApiOperation(value = "商品配置，根据条件获取商品库存数据")
    @RequestMapping(value = "/getProdStockSetListShow", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<JSONObject> getProdStockSetListShow(@RequestParam(value = "skuCodes", defaultValue = "") String skuCodes,
                                                            @RequestParam(value = "releaseAreas", defaultValue = "") String releaseAreas,
                                                            @RequestParam(value = "prodSpecId", defaultValue = "") Long prodSpecId,
                                                            @RequestParam(value = "productId", defaultValue = "") Long productId,
                                                            @RequestParam(value = "channelId", defaultValue = "") Long channelId

    ) {
        String url = HOST_MALL_PRODUCT +SERVICE_NAME+ "/getProdStockSetListShow";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        Map<String,Object> param = Maps.newHashMap();
        param.put("skuCodes",skuCodes);
        param.put("releaseAreas",releaseAreas);
        param.put("prodSpecId",prodSpecId);
        param.put("productId",productId);
        param.put("channelId",channelId);


        param.entrySet().stream().forEach(o -> builder.queryParam(o.getKey(),o.getValue()));
        String restUrl = builder.build().encode().toString();
        log.debug("★★★★★★PmProductController.getProdStockSetListShow   restUrl="+restUrl);

        ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(restUrl, CommonResult.class);
        return responseEntity.getBody();
    }
}
